package com.xk.meeting.dao;

import com.google.gson.internal.$Gson$Preconditions;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.xk.meeting.entity.User;
import com.xk.meeting.utils.ConnectionUtils;
import com.xk.meeting.utils.UtilHelper;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserDaoImpl implements UserDao{
    private JdbcTemplate template = new JdbcTemplate(ConnectionUtils.getDataSource());
    @Override
    public int addUser(User user) {
        String sql = "insert user values(null,?,?,?,?,?,?)";
        return template.update(sql,user.getUsername(),user.getEmail(),user.getPhone(),user.getPassword(), UtilHelper.getTime(),0);
    }

    @Override
    public boolean isExist(String phone) {
        String sql = "select count(id) from user where phone = ?";
        int count = template.queryForObject(sql,Integer.class,phone);
        if(count > 0){
            return  true;
        }else{
            return false;
        }
    }

    @Override
    public User query(String username, String password) {
        String sql = "select * from user where phone = ? and password = ?";
        try{
            return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),username,password);
        } catch (DataAccessException e){
            return null;
        }
    }

    @Override
    public User getUserForPhone(String phone) {
        String sql = "select * from user where phone = ?";
        try{
            return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),phone);
        } catch (DataAccessException e){
            return null;
        }
    }

    @Override
    public int updateName(String username, int id) {
        String sql = "update user set username = ? where id = ?";
        return template.update(sql,username,id);
    }

    @Override
    public String getPasswordById(int id) {
        String sql = "select password from user where id = ?";
        return template.queryForObject(sql,String.class,id);
    }

    @Override
    public int updatePassword(String id, String newpass) {
        String sql = "update user set password = ? where id = ?";
        return template.update(sql,newpass,id);
    }

    @Override
    public String getNameById(int id) {
        String sql = "select username from user where id = ?";
        return template.queryForObject(sql,String.class,id);
    }

    @Override
    public int getStarusById(int uid) {
        String sql = "select status from user where id = ?";
        return template.queryForObject(sql,Integer.class,uid);
    }

    @Override
    public int changeStatus(int uid, int status) {
        String sql = "update user set status = ? where id = ?";
        return template.update(sql,status,uid);
    }

    @Override
    public int getState(int id) {
        String sql = "select status from user where id = ?";
        return template.queryForObject(sql,Integer.class,id);
    }
}
